# Maintainer: Michael Hansen <zrax0111 gmail com>

_realname=unicorn
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-unicorn"
         "${MINGW_PACKAGE_PREFIX}-python2-unicorn"
         "${MINGW_PACKAGE_PREFIX}-python3-unicorn")
pkgver=1.0.1
pkgrel=2
pkgdesc='A lightweight multi-platform, multi-architecture CPU emulator framework based on QEMU'
url='http://www.unicorn-engine.org/index.html'
arch=('any')
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=(python2
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/unicorn-engine/unicorn/archive/${pkgver}.tar.gz")
sha512sums=('edfe1f7bfbc1d20f5b62232057e194a937bc09db686ef2efadb33a54605029a53426432cdb2a29511385aacdb9343b3b3091af50a1909098d7cf6db3429eb966')

prepare() {
  cd "${_realname}-${pkgver}"
  sed 's|-O3|-O2|g' -i Makefile qemu/configure
  sed 's|-g ||g' -i qemu/configure
  sed 's|UNICORN_DEBUG ?= yes|UNICORN_DEBUG ?= no|g' -i config.mk
  cp -ra bindings/python{,3}
  sed -r 's|(python)$|\13|' -i bindings/python3/*.py
}

build() {
  cd "${_realname}-${pkgver}"
  make clean

  UNICORN_QEMU_FLAGS="--python=/usr/bin/python2" \
  make PREFIX="${MINGW_PREFIX}"

  (cd bindings
    python2 const_generator.py python
  )
  (cd bindings/python
    python2 setup.py build
  )
  (cd bindings/python3
    python3 setup.py build
  )
}

package_unicorn() {
  cd "${_realname}-${pkgver}"

  make DESTDIR="${pkgdir}" PREFIX="${MINGW_PREFIX}" install
  install -dm755 "${pkgdir}/${MINGW_PREFIX}/bin"
  mv "${pkgdir}/${MINGW_PREFIX}/lib/unicorn.dll" "${pkgdir}/${MINGW_PREFIX}/bin/libunicorn.dll"
  mv "${pkgdir}/${MINGW_PREFIX}/lib/unicorn.a" "${pkgdir}/${MINGW_PREFIX}/lib/libunicorn.a"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}/${MINGW_PREFIX}/share/licenses/${pkgname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING_GLIB" "${pkgdir}/${MINGW_PREFIX}/share/licenses/${pkgname}/COPYING_GLIB"
}

package_mingw-w64-i686-unicorn()   { package_unicorn; }
package_mingw-w64-x86_64-unicorn() { package_unicorn; }

package_python2-unicorn() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-unicorn")
  cd "${_realname}-${pkgver}/bindings/python"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  python2 setup.py install --root="${pkgdir}" --prefix="${MINGW_PREFIX}" \
    -O1 --skip-build

  install -Dm 644 sample* shellcode.py -t "${pkgdir}/${MINGW_PREFIX}/share/doc/python2-${_realname}/samples"
}

package_mingw-w64-i686-python2-unicorn()   { package_python2-unicorn; }
package_mingw-w64-x86_64-python2-unicorn() { package_python2-unicorn; }

package_python3-unicorn() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3" "${MINGW_PACKAGE_PREFIX}-unicorn")
  cd "${_realname}-${pkgver}/bindings/python3"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  python3 setup.py install --root="${pkgdir}" --prefix="${MINGW_PREFIX}" \
    -O1 --skip-build

  install -Dm 644 sample* shellcode.py -t "${pkgdir}/${MINGW_PREFIX}/share/doc/python3-${_realname}/samples"
}

package_mingw-w64-i686-python3-unicorn()   { package_python3-unicorn; }
package_mingw-w64-x86_64-python3-unicorn() { package_python3-unicorn; }
